<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html>
<html>
<head>
<title></title>
<base href="<%=basePath%>">
<%@ include file="/base/jsp/include.jsp"%>
<script type="text/javascript">
    var grid;
    /* var addFun = function() {
        var dialog = parent.sy.modalDialog({
            title : '添加部门',
            width : 800,
            height : 600,
            url : sy.contextPath + '/go.do?path=deptMsg/deptMsgEdit',
            buttons : [ {
                text : '添加',
                handler : function() {
                    dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
                }
            } ]
        });
    };
    var editFun = function() {
        var rows = grid.datagrid('getSelections');
        if (rows.length != 1) {
            parent.$.messager.w('请选择一条记录进行编辑！');
            return;
        }
        var dialog = parent.sy.modalDialog({
            title : '修改部门信息',
            width : 800,
            height : 600,
            url : sy.contextPath + '/go.do?path=deptMsg/deptMsgEdit&id=' + rows[0].id,
            buttons : [ {
                text : '确定',
                handler : function() {
                    dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
                }
            } ]
        });
    };
    var delFun = function() {
        var rows = grid.datagrid('getSelections');
        if (rows.length == 0) {
            parent.$.messager.w('请选择需要删除的记录！');
            return;
        }
        parent.$.messager.confirm('询问', '您确定要删除此记录？', function(r) {
            if (r) {
                var ids = [];
                for ( var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.id);
                }
                var id = ids.join(',');
                
                $.post(sy.contextPath + '/dept/del.do', {
                    id : id
                }, function() {
                    rows.length = 0;//必须，否则有bug
                    grid.datagrid('reload');
                }, 'json');
            }
        });
    }; */
    var addFun = function() {
        var rows = grid.datagrid('getSelections');
        if (rows.length != 1) {
            parent.$.messager.w('请选择一个父节点！');
            return;
        }
        if(rows[0].level > 1){
            parent.$.messager.w('暂支持2级目录！');
            return;
        }
        var dialog = parent.sy.modalDialog({        	
            title : '添加部门',
            width : 700,
            height : 500,
            url : sy.contextPath + '/go.do?path=deptMsg/deptMsgEdit&parent='+rows[0].id+'&level='+(rows[0].level+1),/* +'&deptNo='+rows[0].deptNo, */
            buttons : [ {
                text : '添加',
                handler : function() {
                    dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$, parent.mainMenu);
                }
            } ]
        });
    };
    
    var editFun = function() {
        var rows = grid.datagrid('getSelections');
        if (rows.length != 1) {
            parent.$.messager.alert('warning', '请选择一条记录进行编辑！','warning');
            return;
        }
        if(rows[0].id == '0'){
            parent.$.messager.alert('warning', '根节点无法编辑！','warning');
            return;
        }
        var dialog = parent.sy.modalDialog({
            title : '编辑菜单',
            width : 700,
            height : 500,
            url : sy.contextPath + '/go.do?path=deptMsg/deptMsgEdit&id=' + rows[0].id,
            buttons : [ {
                text : '编辑',
                handler : function() {
                    dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$, parent.mainMenu);
                }
            } ]
        });
    };
    
    var delFun = function() {
        var rows = grid.datagrid('getSelections');
        if (rows.length == 0) {
            parent.$.messager.alert('warning', '请选择需要删除的记录！','warning');
            return;
        }
        if(rows[0].id == '0'){
            parent.$.messager.alert('warning', '根节点无法编辑！','warning');
            return;
        }
        parent.$.messager.confirm('询问', '您确定要删除此记录？', function(r) {
            if (r) {
                var ids = [];
                for ( var i = 0, l = rows.length; i < l; i++) {
                    var r = rows[i];
                    ids.push(r.id);
                }
                var id = ids.join(',');
                
                $.post(sy.contextPath + '/dept/del.do', {
                    id : id
                }, function() {
                    grid.treegrid('reload',r.parent);
                }, 'json');
            }
        });
    };
   /*  $(function() {
        
        var menu;
        menu={
                url : sy.contextPath + '/dept/list.do',
                toolbar : '#toolbar',
                singleSelect : false,
                columns : [ [ {
                    width : $(this).width() * 0.1,
                    title : '部门编号',
                    field : 'deptNo',
                    align : 'center'
                }, {
                    width : $(this).width()*0.1,
                    title : '部门名称',
                    field : 'deptName',
                    align : 'center',
                }
                ] ]
            };
        
        grid = $('#grid').datagrid(menu);
    }); */
    $(function() {
        grid = $('#grid').treegrid({
            url : sy.contextPath + '/dept/list.do',
            idField : 'id',
            treeField : 'deptName',
            parentField : 'parent',
            toolbar : '#toolbar',
            pagination : false,
            frozenColumns : [ [ {
                width : '250',
                title : '部门',
                align : 'left',
                field : 'deptName'
            } ] ],
            columns : [ [{
                width : '100',
                title : '部门编号',
                field : 'deptNo',
                align : 'center'
            }, {
                width : '200',
                title : '创建时间',
                field : 'createTime',
                align : 'center'
            }, {
                width : '200',
                title : '更新时间',
                field : 'modifyTime',
                align : 'center'
            }
            ]]
        });
    });
</script>
</head>
<body class="easyui-layout" data-options="fit:true,border:false">
    <div id="toolbar">
    
        <table>
            <tr>
                <td>
                    <table>
                        <tr>
                            <td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-note_add',plain:true" onclick="addFun();">添加</a></td>
                            <td><div class="datagrid-btn-separator"></div></td>
                            <!-- <td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-table_edit',plain:true" onclick="editFun();">修改</a></td>
                            <td><div class="datagrid-btn-separator"></div></td> -->
                            <td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-table_delete',plain:true" onclick="delFun();">删除</a></td>
                        </tr>
                    </table>
                </td>
            </tr>
            <!-- <tr>
                <td>
                    <form id="searchForm">
                        <table>
                            <tr>
                                <td>部门名称：</td>
                                <td>
                                    <input type="text" class="easyui-textbox" name="name" style="width: 150px"  />
                                </td>
                                <td>
                                    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom',plain:true" onclick="grid.datagrid('load',sy.serializeObject($('#searchForm')));">过滤</a>
                                    <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'ext-icon-zoom_out',plain:true" onclick="$('#searchForm input').val('');grid.datagrid('load',{});">重置过滤</a>
                                </td>
                            </tr>
                        </table>
                    </form>
                </td>
            </tr> -->
        </table>
    </div>
    <div data-options="region:'center',fit:true,border:false">
        <table id="grid" data-options="fit:true,border:false"></table>
    </div>
</body>
</html>